<?php

namespace App\Http\Controllers\product;

use App\Models\Comment;
use App\Models\Image;
use App\Models\Product;
use App\Models\Type;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class ProductController extends Controller
{
    //
    public function index($id = 2)
    {
        $image = Image::where('owner_id', $id)->get();
        $product = Product::where('id', $id)->first();
        $comment = Product::find($product->id)->comment;
        foreach ($comment as $v){
            $v->replay=$v->find($v->id)->replays;
            $id=$v->find($v->id)->user->id;
            $v->user=User::find($id)->user_info;
        }
        if ($product == null) {
            return null;
        }
        return view('home.product.productinfo', ['image' => $image, 'product' => $product,'comment'=>$comment]);
    }

    public function redata(Request $request)
    {
        //dd($request->input('store_id'));
//        if ($request->has('store_id')) {
//            $store_id = $request->input('store_id');
//            if ($request->has('where')){
//                $value=$request->input('value');
//                $where = $request->input('where');
//                $list = Product::where('store_id', $store_id)
//                    ->where($where, $value)
//                    ->paginate(6);
//            }else{
//                $list = Product::where('store_id', $store_id)
//                    ->paginate(6);
//            }
//
//
//        } else {
//            if ($request->has('where')){
//                $where = $request->input('where');
//                $value=$request->input('value');
//                $list = Product::where($where, $value)
//                    ->paginate(6);
//            }else{
//                $list = Product::paginate(6);
//            }
//
//        }
//        return $list->toJson();
    }

    public function readata(Request $request)
    {
        //dd($request->input('store_id'));
        if ($request->has('store_id')) {
            $store_id = $request->input('store_id');
            $list = Product::where('store_id', $store_id)
                ->paginate(6);
        } else {
            $list = Product::paginate(6);
        }
        return $list->toJson();
    }

    public function list(Request $request)
    {
        $store_id=session('location')['store_id'];
        $label = $request->input('label');
        $ob = new type();
        $res = $ob->getone();
        $pro = Product::where('label', 'like', "%$label%")->where('store_id',$store_id)->where('status',1)->orderBy('sale', 'desc')->paginate(7);
//        dd($pro);
        return view('home.product.productlist', ['pro' => $pro, 'res' => $res, 'label' => $label]);
    }

    public function selpro(Request $request)
    {
        $store_id=session('location')['store_id'];
        $type = $request->input('type');
        $order = $request->input('order');
        $asc = $request->input('asc');
        $list = Product::where('label', 'like', "%$type%")->where('store_id',$store_id)->where('status',1)->orderBy($order, $asc)->paginate(5);
        return $list->toJSON();
    }

}
